package com.monitor.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.monitor.dao.RemoteSmsDao;
import com.monitor.po.RemoteSms;
import com.monitor.util.Pager;

@SuppressWarnings("unchecked")
public class RemoteSmsDaoImpl implements RemoteSmsDao {
	private SessionFactory sessionFactory;

	@Override
	public void addRemoteSms(RemoteSms sms) throws Exception {
		try {
			this.sessionFactory.getCurrentSession().persist(sms);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while excuting addRemoteSms");
		}
	}

	@Override
	public int deleteRemoteSmsById(long id) throws Exception {
		try {
			Query query = sessionFactory.getCurrentSession().createQuery("delete from RemoteSms r where r.id=:id").setLong("id", id);
			return query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while excuting deleteRemoteSmsById");
		}
	}

	@Override
	public int deleteRemoteSms(long[] ids) throws Exception {
		try {
			StringBuffer sql = new StringBuffer("delete from RemoteSms r where");
			int length = ids.length;
			for (int i = 0; i < length; i++) {
				sql.append(" r.id=?");
				if (i != length - 1) {
					sql.append(" or ");
				}
			}
			Query query = sessionFactory.getCurrentSession().createQuery(sql.toString());
			for (int i = 0; i < length; i++) {
				long id = ids[i];
				query.setLong(i, id);
			}
			return query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while excuting deleteRemoteSms");
		}
	}

	@Override
	public int deleteRemoteSmsByUid(long uid) throws Exception {
		try {
			Query query = sessionFactory.getCurrentSession().createQuery("delete from RemoteSms r where r.ownerUserId=:uid").setLong("uid", uid);
			return query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while excuting deleteRemoteSmsByUid");
		}
	}

	@Override
	public Pager<RemoteSms> getRemoteSmsByUid(long uid, int currentPage, int pageSize) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			Query query = sesion.createQuery("select count(*) from RemoteSms r where r.excuted=:excuted and r.ownerUserId=:uid").setBoolean("excuted", false).setLong("uid", uid);
			int totalCount = ((Number) query.uniqueResult()).intValue();
			int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
			List<RemoteSms> list = sesion.createQuery("from RemoteSms r where r.excuted=:excuted and r.ownerUserId=:uid order by r.id desc").setBoolean("excuted", false).setLong("uid", uid)
					.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
			Pager<RemoteSms> pager = new Pager<RemoteSms>(list, currentPage, totalCount, pageCount);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting getRemoteSmsByUid()....");
		}
	}

	@Override
	public int updateRemoteSmsExcutedStatus(long[] ids, boolean excuted) throws Exception {
		try {
			StringBuffer sql = new StringBuffer("update RemoteSms r set r.excuted=?  where");
			int length = ids.length;
			for (int i = 0; i < length; i++) {
				sql.append(" r.id=? ");
				if (i != length - 1) {
					sql.append(" or ");
				}
			}
			Query query = sessionFactory.getCurrentSession().createQuery(sql.toString());
			query.setBoolean(0, excuted);
			for (int i = 0; i < length; i++) {
				long id = ids[i];
				query.setLong(i + 1, id);
			}
			return query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while excuting updateRemoteSmsExcutedStatus");
		}
	}

	@Override
	public List<RemoteSms> getRemoteSmsByUid(long uid, int pageSize) throws Exception {
		try {
			return sessionFactory.getCurrentSession().createQuery("from RemoteSms r where r.ownerUserId=:uid and r.excuted=:excuted").setLong("uid", uid).setBoolean("excuted", false)
					.setMaxResults(pageSize).list();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while excuting getRemoteSmsByUid");
		}
	}

	@Override
	public int getSentRemoteSmsCount(long uid) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			Query query = sesion.createQuery("select count(*) from RemoteSms r where r.excuted=:excuted and r.ownerUserId=:uid").setBoolean("excuted", true).setLong("uid", uid);
			int totalCount = ((Number) query.uniqueResult()).intValue();
			return totalCount;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while excuting getSentRemoteSmsCount");
		}
	}

	@Override
	public int getUnSentRemoteSmsCount(long uid) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			Query query = sesion.createQuery("select count(*) from RemoteSms r where r.excuted=:excuted and r.ownerUserId=:uid").setBoolean("excuted", false).setLong("uid", uid);
			int totalCount = ((Number) query.uniqueResult()).intValue();
			return totalCount;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured while excuting getUnSentRemoteSmsCount");
		}
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
